/**
 * 回收宝价格选项数据组装
 * @param {Array} list 原始选项数据
 * @param {Array} defaultOptionList 原始选项数据
 * @param {Array} selectIds 选中选项ID列表
 */
export function computedSelectData (list = [], defaultOptionList = [], selectIds = []) {
  let defaultOptionQuestion = []
  // 初始化选项数据
  list.forEach((item, index) => {
    if (index === 0 || selectIds.length) {
      item.show_status = true
    } else {
      item.show_status = false
    }
    item.questions.forEach((child, idx) => {
      if ((index === 0 && idx === 0) || selectIds.length) {
        child.show_status = true
      } else {
        child.show_status = false
      }
      child.fold = false
      child.selectName = ''
      child.single = child.single || (child.conftype === '3' ? '2' : '1')
      if (defaultOptionQuestion.includes(child.id)) {
        child.show_status = false
        child.default_option = true
        if (item.questions.every(e => e.default_option)) {
          item.show_status = false
        }
      } else {
        child.default_option = false
      }
      child.question.forEach(v => {
        v.selected = false
        v.single = v.single || (child.conftype === '3' ? '2' : '1')
        // 开始处理选中列表数据
        if (selectIds.includes(v.id) && !defaultOptionQuestion.includes(child.id)) {
          v.selected = true
          child.show_status = true
          item.show_status = true
          if (child.single !== '2') {
            child.selectName = v.name
            child.fold = true
          } else {
            child.selectName = child.question.filter(e => e.selected).map(e => e.name).join('、')
          }
        }
        // 寻找默认选项的答案项
        if (defaultOptionList && defaultOptionList.length) {
          const optionInfo = defaultOptionList.find(item => item.aId === v.id)
          if (optionInfo) {
            // 标识联动问题项的位置和联动的目标数据
            v.default_option_index = `${index}_${idx}`
            v.default_option_questions = optionInfo.defaultAid.map(item => item.qId)
            // 命中改答案项，记录联动的选项
            if (selectIds.includes(v.id)) {
              defaultOptionQuestion = defaultOptionQuestion.concat(v.default_option_questions)
            }
          }
        }
      })
    })
  })
  return list
}

/**
 * 第三方穿戴选项转回收宝价格选项
 * @param {Array} list 原始选项数据
 */
export function computedWearData (list = []) {
  const finalList = []
  const singleOptions = []
  const multipleOptions = []
  list.forEach(item => {
    const multiple = item.type === '2'
    const data = {
      id: item.qid,
      name: item.qname,
      conftype: multiple ? '3' : '1',
      question: item.anslist.map(child => {
        return {
          id: child.aid,
          name: child.aname,
          single: multiple ? '2' : '1'
        }
      }),
      single: multiple ? '2' : '1'
    }
    if (multiple) {
      multipleOptions.push(data)
    } else {
      singleOptions.push(data)
    }
  })
  if (singleOptions.length) {
    finalList.push({
      questions: singleOptions
    })
  }
  if (multipleOptions.length) {
    finalList.push({
      conftype: '3',
      questions: multipleOptions
    })
  }
  return finalList
}